package com.fjnldx.mes.service.historicalData;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjnldx.mes.domain.domainReq.HistoryDataQueryReq;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.pojo.HistoricalDataQueryInfo;
import com.fjnldx.mes.mapper.historicalData.HistoricalDataMapper;
import com.fjnldx.mes.mapper.productionFigures.ProductionFiguresMapper;
import com.fjnldx.mes.utils.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HistoricalDataService {

    @Autowired
    private Common common;
    @Autowired
    private ProductionFiguresMapper productionFiguresMapper;
    @Autowired
    private HistoricalDataMapper historicalDataMapper;

    /**
     * 各工序各设备某参数历史数据查询
     *
     * @param historyDataQueryReq
     * @return
     */
    public Result getEmploymentHistoryData(HistoryDataQueryReq historyDataQueryReq, Page page) {
        List<String> tableNames = new ArrayList<>();
        List<HistoricalDataQueryInfo> tableNamesByQuery = new ArrayList<>();
        String tablePrefix = null;

        // 判断工序, 根据工序,日期获取对应的历史表名称
        String gongxu = historyDataQueryReq.getGongxu();

        // 获取两个日期间的每天的日期格式化字符串
        List<String> daysBySection = common.getDaysBySectionAndFormat(historyDataQueryReq.getStartTime(), historyDataQueryReq.getEndTime(), "yyyyMMdd");
        if(daysBySection.size() > 3){
            return Result.error("只能查询3天的历史数据!");
        }

        daysBySection.forEach((dayStr) -> {
            tableNames.add(dayStr);
        });

        if ("细纱".equals(gongxu)) {
            tablePrefix = "history_xs_";
        }
        if ("粗纱".equals(gongxu)) {
            tablePrefix = "history_cs_";
        }
        if ("新粗纱".equals(gongxu)) {
            tablePrefix = "history_cs2_";
        }
        if ("抓棉".equals(gongxu)) {
            tablePrefix = "history_zmj_";
        }
        if ("络筒".equals(gongxu)) {
            tablePrefix = "history_lt_";
        }
        if ("梳棉a".equals(gongxu)) {
            tablePrefix = "history_sma_";
        }
        if ("梳棉".equals(gongxu)) {
            tablePrefix = "history_sm_";
        }
        if ("精开棉".equals(gongxu)) {
            tablePrefix = "history_kmj_";
        }
        if(tablePrefix == null){
            return Result.error("暂不支持该工序的历史数据查询!");
        }

        tableNamesByQuery = this.getTableNamesByQuery(tableNames, tablePrefix);
        if(tableNamesByQuery.size() == 0){
            return Result.error("该数据不存在!");
        }
        historyDataQueryReq.setTableNames(tableNamesByQuery);
        IPage<List<Map<String, Object>>> historyData =  historicalDataMapper.getHistoryData(page, historyDataQueryReq);

        return Result.success(historyData);
    }

    private List<HistoricalDataQueryInfo> getTableNamesByQuery(List<String> tableNames, String tablePrefix){
        List<HistoricalDataQueryInfo> tableNamesByQuery = new ArrayList<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime now = LocalDateTime.now();
        tableNames.forEach(table -> {
            String tableName = tablePrefix + table;
            if(common.checkTableIsExits(tableName)){
                HistoricalDataQueryInfo dataQueryInfo = new HistoricalDataQueryInfo();
                dataQueryInfo.setTableName(tableName);
                dataQueryInfo.setConditionStr(table);
                tableNamesByQuery.add(dataQueryInfo);
            }else if(dateTimeFormatter.format(now).equals(table)){
                String todayTableName = common.getTodayTableNameByPrefix(tablePrefix);
                if(todayTableName != null){
                    HistoricalDataQueryInfo dataQueryInfo = new HistoricalDataQueryInfo();
                    dataQueryInfo.setTableName(todayTableName);
                    dataQueryInfo.setConditionStr(table);
                    tableNamesByQuery.add(dataQueryInfo);
                }
            }
        });
        return tableNamesByQuery;
    }
}
